package com.zjsru.oneDay;

import java.util.Arrays;

/**
 * 75. 颜色分类
 * @Author: cookLee
 * @Date: 2025-05-17 
 */
public class SortColors {

    /**
     * 主
     * \
     * 输入：nums = [2,0,2,1,1,0]
     * 输出：[0,0,1,1,2,2]
     * \
     * 输入：nums = [2,0,1]
     * 输出：[0,1,2]
     * \
     * @param args args
     */
    public static void main(String[] args) {
        SortColors sortColors = new SortColors();
        int[] nums = new int[]{2, 0, 2, 1, 1, 0};
        sortColors.sortColors(nums);
        System.out.println(Arrays.toString(nums));
    }

    public void sortColors(int[] nums) {
        int len = nums.length;
        //0的下标和
        int ptr = 0;
        for (int i = 0; i < len; i++) {
            if (nums[i] == 0) {
                int temp = nums[i];
                nums[i] = nums[ptr];
                nums[ptr] = temp;
                ptr++;
            }
        }
        //处理1的位置
        for (int i = ptr; i < len; i++) {
            if (nums[i] == 1) {
                int temp = nums[i];
                nums[i] = nums[ptr];
                nums[ptr] = temp;
                ptr++;
            }
        }
    }
}
